package com.itheima.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{
    private BrandService brandService = new BrandServiceImpl();

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Brand> brands = brandService.selectAll();
        String jsonString = JSON.toJSONString(brands);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        Brand brand = JSON.parseObject(s, Brand.class);
        brandService.add(brand);
        response.getWriter().write("success");
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        Brand brand = JSON.parseObject(s, Brand.class);
        brandService.update(brand);
        response.getWriter().write("success");
    }


    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);
        brandService.deleteById(id);
        response.getWriter().write("success");
    }

    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        int[] ids = JSON.parseObject(s, int[].class);
        brandService.deleteByIds(ids);
        response.getWriter().write("success");
    }

    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        String _currentPage = request.getParameter("currentPage");
        String _size = request.getParameter("PageSize");
        int currentPage = Integer.parseInt(_currentPage);
        int size = Integer.parseInt(_size);
        PageBean<Brand> brandPageBean = brandService.selectByPages(currentPage, size);
        String jsonString = JSON.toJSONString(brandPageBean);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void selectByPageAndConnection(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{

        System.out.println("111111111");

        String _currentPage = request.getParameter("currentPage");
        String _PageSize = request.getParameter("PageSize");
        int currentPage = Integer.parseInt(_currentPage);
        int PageSize = Integer.parseInt(_PageSize);

        BufferedReader br = request.getReader();
        String s =br.readLine();
        Brand brand = JSON.parseObject(s, Brand.class);
        System.out.println(brand);
        PageBean<Brand> brandPageBean = brandService.selectByPageAndConnection(currentPage, PageSize, brand);
        String jsonString = JSON.toJSONString(brandPageBean);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

}
